package com.qingmu.club.minaapi.custom.logic;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qingmu.club.minaapi.plus.entity.ClubShop;
import com.qingmu.club.minaapi.plus.entity.ClubStaff;
import com.qingmu.club.minaapi.plus.entity.ClubStaffShop;
import com.qingmu.club.minaapi.plus.service.ClubShopService;
import com.qingmu.club.minaapi.plus.service.ClubStaffService;
import com.qingmu.club.minaapi.plus.service.ClubStaffShopService;
import com.qingmu.club.minaapi.utils.ConvertUtil;
import com.qingmu.club.minaapi.utils.ResponseObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

@Component
public class ClubStaffShopLogic {

    @Autowired
    private ClubStaffShopService clubStaffShopService;

    @Autowired
    private ClubShopService clubShopService;

    @Autowired
    private ClubStaffService clubStaffService;
    /**
     * 根据门店id查询教练总数
     * @return
     */
    public int queryCoachSum (String shopId){
        QueryWrapper<ClubStaffShop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ClubStaffShop.SHOP_ID,shopId);
        queryWrapper.eq(ClubStaffShop.STAFF_TAG,0);//人员角色 0-教练 1-课程顾问 2-店主 3-管理员
        return this.clubStaffShopService.count(queryWrapper);
    }

    /**
     * 查询教练绑定的门店
     * @param coachId
     * @return
     */
    public List<HashMap<String,Object>> queryShopList(String coachId, String staffTag, ClubStaff clubStaff){
        Integer Tag = 0 ;//  * 人员角色0-教练1-课程顾问2-店主3-管理员
        if(staffTag.equals("isCoach")){
            Tag=0;
        }else if(staffTag.equals("isCounselor")){
            Tag=1;
        }else if(staffTag.equals("isShopowner")){
            Tag=2;
        }else {
            Tag=3;
        }

        QueryWrapper<ClubStaffShop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ClubStaffShop.STAFF_ID,coachId).eq(ClubStaffShop.STAFF_TAG,Tag);
        List<ClubStaffShop> list=this.clubStaffShopService.list(queryWrapper);
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        List<HashMap<String,Object>> list1 = new ArrayList<>();
        boolean falg = false;
        for(ClubStaffShop clubStaffShop : list){
            ClubShop clubShop = this.clubShopService.getById(clubStaffShop.getShopId());
            if(clubShop.getId().equals(clubStaff.getDefaultShopId())){
                falg=true;
            }
            HashMap<String,Object> map = ConvertUtil.objectToMap(clubStaffShop);
            map.put("shopBeginHours",clubShop.getShopBeginHours());
            map.put("shopEndHours",clubShop.getShopEndHours());
            map.put("address",clubShop.getJointAddress()+" "+clubShop.getAddress());
            list1.add(map);
        }

        if(StringUtils.isEmpty(clubStaff.getDefaultShopId())) { //给一个默认门店
            clubStaff.setDefaultShopId(list.get(0).getShopId());
            clubStaff.setDefaultShopName(list.get(0).getShopName());
            this.clubStaffService.updateById(clubStaff);
        }else {
            if (!falg) {
                clubStaff.setDefaultShopId(list.get(0).getShopId());
                clubStaff.setDefaultShopName(list.get(0).getShopName());
                this.clubStaffService.updateById(clubStaff);
            }
        }
        return list1;
    }
}
